// pages/rate/rate.js
const ipUtil = require("../../utils/hostUtil");
const requestUtil = require("../../utils/requestUtil");
Page({

  /**
   * 页面的初始数据
   */
  data: {
	flag:[0, 0, 0],
    startext: ['', '', ''],
    stardata: [1, 2, 3, 4, 5],
	id:'',
	discription:'',
	imgs:[],
	level:'',
	list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this
	  console.log(options)
	  let id = options.id
	  that.setData({
		  id:id
	  })
	  wx.request({
	  		  url:`${ipUtil.host}${requestUtil.getfruit}`,
	  		  method:'POST',
	  		  data:{id:id},
	  		  header:requestUtil.header,
	  		  success(res) {
	  		  	console.log(res)
				that.setData({
					list:res.data.data
				})
	  		  }
	  })
	  // this.loadPage(skuId)
	  // console.log(skuId)
  },
  changeColor: function (e) {
        var index = e.currentTarget.dataset.index;
        var level = e.currentTarget.dataset.no;
        var a = 'flag[' + index + ']';
        var b = 'startext[' + index + ']';
        var that = this;
		that.setData({
			level:level
		})
		console.log(level)
        if(level == 1) {
          that.setData({
            [a]: 1,
            [b]: '非常不满意'
          });
        } else if (level == 2){
          that.setData({
            [a]: 2,
            [b]: '不满意'
          });
        } else if (level == 3) {
          that.setData({
            [a]: 3,
            [b]: '一般'
          });
        } else if (level == 4) {
          that.setData({
            [a]: 4,
            [b]: '满意'
          });
        } else if (level == 5) {
          that.setData({
            [a]: 5,
            [b]: '非常满意'
          });
        }
    },
	
	//上传图片
	chooseImageTap: function () {
	  var that = this;
	  
	  wx.showActionSheet({
	    itemList: ['从相册中选择', '拍照'],
	    success: function (res) {
	      if (!res.cancel) {
	        if (res.tapIndex == 0) {
	          that.chooseWxImage('album')
	        } else if (res.tapIndex == 1) {
	          that.chooseWxImage('camera')
	        }
	      }
	    }
	  })
	},
	// 图片本地路径
	chooseWxImage: function (type) {
	  var that = this;
	  var num = that.data.imgs.length
	  wx.chooseImage({
		count:3-num,
	    sizeType: ['original', 'compressed'],
	    sourceType: [type],
	    success: function (res) {
			var tempFilePaths = res.tempFilePaths
			that.upImgs(tempFilePaths)
	      // console.log(res.tempFilePaths[0]);
	      // that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
	    }
	  }) 
	},
	//上传服务器
	upImgs: function (data) {
	  var that = this;
	  for(let i=0;i<data.length;i++){
		  wx.uploadFile({
		    url: `${ipUtil.host}${requestUtil.upload}`,
		    filePath: data[i],
		  	method:'POST',
		    name: 'file',
		    header:{'content-type': 'application/x-www-form-urlencoded'},
		    success: function (res) {
		      console.log(res) //接口返回网络路径
		      var res_data = JSON.parse(res.data)
		      console.log(res_data.url)
			  var imgUrl = res_data.url
			  var imgs = that.data.imgs
			  imgs.push(imgUrl)
		        that.setData({
		         imgs
		        })
				console.log(that.data.imgs)
		       //  console.log(that.data.url)
		    }
		  })
	  }
	},
	
	reason: function (e) {
	     let {value} = e.detail;
	     this.setData({
	       discription: value
	     })
	 },
	 
	//发表
	bindFormSubmit:function(){
		if(this.data.discription){
			let token = wx.getStorageSync('token')
			let {id,discription,imgs,level} = this.data
			wx.request({
				url:`${ipUtil.host}${requestUtil.evaluation}`,
				method:'POST',
				header:requestUtil.header,
				data:{
					orderVoId:id,
					level:level,
					discription:discription,
					imgs:imgs,
					token:token
				},
				success(res) {
					wx.navigateBack({
						delta:1
					})
					console.log(res)
				}
			})
		}else{
			wx.showToast({
				title:'评价内容不能为空'
			})
		}
		
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.changeColor()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})